package com.huluobo.a2202mvvm_im_douyindemo.mine;

import androidx.lifecycle.MutableLiveData;

import com.huluobo.a2202mvvm_im_douyindemo.adapter.UpdateIconBeanResult;
import com.huluobo.a2202mvvm_im_douyindemo.bean.LoginBeanResult;
import com.huluobo.a2202mvvm_im_douyindemo.mvvmBase.BaseRepo;
import com.hyphenate.EMCallBack;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MultipartBody;

/**
 * Created by LC on 2024/4/22.
 */
public class MineRepo extends BaseRepo {
    public void logout(MutableLiveData<String> success, MutableLiveData<String> failed) {
        emClient.logout(true, new EMCallBack() {
            @Override
            public void onSuccess() {
                success.postValue("退出成功");
            }

            @Override
            public void onError(int code, String error) {
                failed.postValue(error);
            }
        });
    }

    public void getUserIcon(String username, MutableLiveData<LoginBeanResult.LoginBean> success,
                            MutableLiveData<String> failed) {
        userDao.queryUserInfoByName(username)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LoginBeanResult.LoginBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(LoginBeanResult.LoginBean loginBean) {
                        success.setValue(loginBean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void updateUserIcon(MultipartBody.Part part, MutableLiveData<UpdateIconBeanResult> success,
                               MutableLiveData<String> failed) {
        apiService.updateUserIcon(part)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UpdateIconBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(UpdateIconBeanResult updateIconBeanResult) {
                        success.setValue(updateIconBeanResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
